//
//  PostRequestExample.m
//  OCStarterProject
//
//  Created by huaya on 2025/10/5.
//

#import "PostRequestExample.h"
#import "APIManager.h"
#import "APIURLManager.h"

@implementation PostRequestExample

#pragma mark - 邮箱登录示例
+ (void)emailLoginExample {
    NSLog(@"=== 邮箱登录POST请求示例 ===");
    
    // 准备请求参数
    NSDictionary *parameters = @{
        @"email": @"user@example.com",
        @"password": @"your_password",
        @"deviceId": @"device_unique_id"
    };
    
    // 执行POST请求
    [APIManager sendPostRequest:[APIURLManager emailLogin]
                     parameters:parameters
                          model:nil
                        success:^(BOOL success, id responseData, id requestModel) {
        if (success) {
            NSLog(@"✅ 邮箱登录成功，响应数据: %@", responseData);
            
            // 处理登录成功逻辑
            if ([responseData isKindOfClass:[NSDictionary class]]) {
                NSDictionary *responseDict = (NSDictionary *)responseData;
                NSString *token = responseDict[@"token"];
                NSDictionary *userInfo = responseDict[@"userInfo"];
                
                if (token) {
                    NSLog(@"🔑 登录成功，获取到token: %@", token);
                    // 保存token到本地
                    [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"userToken"];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                }
            }
        } else {
            NSLog(@"❌ 请求失败");
            APIResponse *responseModel = (APIResponse *)requestModel;
            NSLog(@"错误信息: %@", responseModel.message);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"❌ 网络请求失败: %@", error.localizedDescription);
    }];
}

#pragma mark - 发送邮箱验证码示例
+ (void)sendEmailValidCodeExample {
    NSLog(@"=== 发送邮箱验证码POST请求示例 ===");
    
    NSDictionary *parameters = @{
        @"email": @"user@example.com",
        @"type": @1  // 1: 注册, 2: 重置密码, 3: 修改邮箱
    };
    
    [APIManager sendPostRequest:[APIURLManager sendEmailValidCode]
                     parameters:parameters
                          model:nil
                        success:^(BOOL success, id responseData, id requestModel) {
        if (success) {
            NSLog(@"✅ 验证码发送成功: %@", responseData);
        } else {
            NSLog(@"❌ 验证码发送失败");
            APIResponse *responseModel = (APIResponse *)requestModel;
            NSLog(@"错误信息: %@", responseModel.message);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"❌ 网络请求失败: %@", error.localizedDescription);
    }];
}

#pragma mark - 修改用户信息示例
+ (void)changeUserInfoExample {
    NSLog(@"=== 修改用户信息POST请求示例 ===");
    
    NSDictionary *parameters = @{
        @"nickname": @"新的昵称",
        @"gender": @1,  // 1: 男, 2: 女
        @"birthday": @"1990-01-01",
        @"signature": @"这是我的个性签名"
    };
    
    [APIManager sendPostRequest:[APIURLManager changeUserInfo]
                     parameters:parameters
                          model:nil
                        success:^(BOOL success, id responseData, id requestModel) {
        if (success) {
            NSLog(@"✅ 用户信息修改成功: %@", responseData);
        } else {
            NSLog(@"❌ 用户信息修改失败");
            APIResponse *responseModel = (APIResponse *)requestModel;
            NSLog(@"错误信息: %@ (错误码: %ld)", responseModel.message, (long)responseModel.code);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"❌ 网络请求失败: %@", error.localizedDescription);
    }];
}

#pragma mark - 收藏书籍示例
+ (void)collectBookExample {
    NSLog(@"=== 收藏书籍POST请求示例 ===");
    
    NSDictionary *parameters = @{
        @"worksId": @"123456",  // 作品ID
        @"type": @1  // 收藏类型
    };
    
    [APIManager sendPostRequest:[APIURLManager collectBook]
                     parameters:parameters
                          model:nil
                        success:^(BOOL success, id responseData, id requestModel) {
        if (success) {
            NSLog(@"✅ 书籍收藏成功: %@", responseData);
        } else {
            NSLog(@"❌ 书籍收藏失败");
            APIResponse *responseModel = (APIResponse *)requestModel;
            NSLog(@"错误信息: %@", responseModel.message);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"❌ 网络请求失败: %@", error.localizedDescription);
    }];
}

#pragma mark - 点赞商品示例
+ (void)likeGoodsExample {
    NSLog(@"=== 点赞商品POST请求示例 ===");
    
    NSDictionary *parameters = @{
        @"goodsId": @"789012",  // 商品ID
        @"type": @1  // 点赞类型
    };
    
    [APIManager sendPostRequest:[APIURLManager likeGoods]
                     parameters:parameters
                          model:nil
                        success:^(BOOL success, id responseData, id requestModel) {
        if (success) {
            NSLog(@"✅ 商品点赞成功: %@", responseData);
        } else {
            NSLog(@"❌ 商品点赞失败");
            APIResponse *responseModel = (APIResponse *)requestModel;
            NSLog(@"错误信息: %@", responseModel.message);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"❌ 网络请求失败: %@", error.localizedDescription);
    }];
}

#pragma mark - 评论商品示例
+ (void)commentGoodsExample {
    NSLog(@"=== 评论商品POST请求示例 ===");
    
    NSDictionary *parameters = @{
        @"goodsId": @"789012",  // 商品ID
        @"content": @"这个商品很不错，推荐购买！",
        @"rating": @5  // 评分 1-5
    };
    
    [APIManager sendPostRequest:[APIURLManager commentGoods]
                     parameters:parameters
                          model:nil
                        success:^(BOOL success, id responseData, id requestModel) {
        if (success) {
            NSLog(@"✅ 商品评论成功: %@", responseData);
        } else {
            NSLog(@"❌ 商品评论失败");
            APIResponse *responseModel = (APIResponse *)requestModel;
            NSLog(@"错误信息: %@", responseModel.message);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"❌ 网络请求失败: %@", error.localizedDescription);
    }];
}

#pragma mark - 使用示例
+ (void)runAllExamples {
    NSLog(@"开始执行所有POST请求示例...");
    
    // 按顺序执行示例（实际使用中应根据业务逻辑调用）
    // [self sendEmailValidCodeExample];
    // [self emailLoginExample];
    // [self changeUserInfoExample];
    // [self collectBookExample];
    // [self likeGoodsExample];
    // [self commentGoodsExample];
    // [self taskSignInExample];
    // [self quickRequestExample];
    
    NSLog(@"POST请求示例代码已准备就绪，请根据需要调用相应方法。");
}

@end
